package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.MallOrderAfterBo;
import com.rla.domain.vo.MallOrderAfterVo;
import com.rla.domain.MallOrderAfter;
import com.rla.mapper.MallOrderAfterMapper;
import com.rla.service.IMallOrderAfterService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单售后信息Service业务层处理
 *
 * @author rla
 * @date 2025-09-05
 */
@RequiredArgsConstructor
@Service
public class MallOrderAfterServiceImpl implements IMallOrderAfterService {

    private final MallOrderAfterMapper baseMapper;

    /**
     * 查询订单售后信息
     */
    @Override
    public MallOrderAfterVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单售后信息列表
     */
    @Override
    public TableDataInfo<MallOrderAfterVo> queryPageList(MallOrderAfterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOrderAfter> lqw = buildQueryWrapper(bo);
        Page<MallOrderAfterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单售后信息列表
     */
    @Override
    public List<MallOrderAfterVo> queryList(MallOrderAfterBo bo) {
        LambdaQueryWrapper<MallOrderAfter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallOrderAfter> buildQueryWrapper(MallOrderAfterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOrderAfter> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), MallOrderAfter::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyType()), MallOrderAfter::getApplyType, bo.getApplyType());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyReason()), MallOrderAfter::getApplyReason, bo.getApplyReason());
        lqw.eq(bo.getRefundNum() != null, MallOrderAfter::getRefundNum, bo.getRefundNum());
        lqw.eq(bo.getApplyAmount() != null, MallOrderAfter::getApplyAmount, bo.getApplyAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyInstructions()), MallOrderAfter::getApplyInstructions, bo.getApplyInstructions());
        lqw.eq(StringUtils.isNotBlank(bo.getRefundWay()), MallOrderAfter::getRefundWay, bo.getRefundWay());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptWay()), MallOrderAfter::getReceiptWay, bo.getReceiptWay());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartment()), MallOrderAfter::getDepartment, bo.getDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getBusiness()), MallOrderAfter::getBusiness, bo.getBusiness());
        lqw.eq(StringUtils.isNotBlank(bo.getReviewResult()), MallOrderAfter::getReviewResult, bo.getReviewResult());
        lqw.eq(StringUtils.isNotBlank(bo.getReviewReason()), MallOrderAfter::getReviewReason, bo.getReviewReason());
        lqw.eq(StringUtils.isNotBlank(bo.getIsRefund()), MallOrderAfter::getIsRefund, bo.getIsRefund());
        lqw.eq(bo.getFgcCreatedate() != null, MallOrderAfter::getFgcCreatedate, bo.getFgcCreatedate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcLastmodifier()), MallOrderAfter::getFgcLastmodifier, bo.getFgcLastmodifier());
        lqw.eq(bo.getFgcLastmodifydate() != null, MallOrderAfter::getFgcLastmodifydate, bo.getFgcLastmodifydate());
        lqw.eq(StringUtils.isNotBlank(bo.getIsReport()), MallOrderAfter::getIsReport, bo.getIsReport());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcCreator()), MallOrderAfter::getFgcCreator, bo.getFgcCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcUpdatehelp()), MallOrderAfter::getFgcUpdatehelp, bo.getFgcUpdatehelp());
        lqw.eq(bo.getFgcRowversion() != null, MallOrderAfter::getFgcRowversion, bo.getFgcRowversion());
        return lqw;
    }

    /**
     * 新增订单售后信息
     */
    @Override
    public Boolean insertByBo(MallOrderAfterBo bo) {
        MallOrderAfter add = BeanUtil.toBean(bo, MallOrderAfter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单售后信息
     */
    @Override
    public Boolean updateByBo(MallOrderAfterBo bo) {
        MallOrderAfter update = BeanUtil.toBean(bo, MallOrderAfter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOrderAfter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单售后信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
